package com.dzt.androidframework.database.taskdetail;

import android.app.Activity;
import android.text.TextUtils;

import com.dzt.androidframework.constant.AppConstant;
import com.dzt.androidframework.database.javabean.Task;
import com.dzt.androidframework.database.tasks.TasksDataSource;
import com.dzt.androidframework.utils.JLogKit;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by M02323 on 2017/6/12.
 */

public class TaskDetailPresenter implements TaskDetailContract.Presenter{

	private final TasksDataSource tasksDataSource;
	private final TaskDetailContract.View taskDetailView;
	private String taskId;

	public TaskDetailPresenter(String taskId,
							   TasksDataSource tasksRepository,
							   TaskDetailContract.View taskDetailView) {
		this.taskId = taskId;
		this.tasksDataSource = checkNotNull(tasksRepository, "tasksRepository cannot be null!");
		this.taskDetailView = checkNotNull(taskDetailView, "taskDetailView cannot be null!");
	}
	
	@Override
	public void start() {
		openTask();
	}

	private void openTask() {
		if (TextUtils.isEmpty(taskId)) {
			taskDetailView.showMissingTask();
			return;
		}

		taskDetailView.setLoadingIndicator(true);
		tasksDataSource.getTask(taskId, new TasksDataSource.GetTaskCallback() {
			@Override
			public void onTaskLoaded(Task task) {
				// The view may not be able to handle UI updates anymore
				if (!taskDetailView.isActive()) {
					return;
				}
				taskDetailView.setLoadingIndicator(false);
				if (null == task) {
					taskDetailView.showMissingTask();
				} else {
					showTask(task);
				}
			}

			@Override
			public void onDataNotAvailable() {
				// The view may not be able to handle UI updates anymore
				if (!taskDetailView.isActive()) {
					return;
				}
				taskDetailView.showMissingTask();
			}
		});
	}

	@Override
	public void result(int requestCode, int resultCode) {
		if (requestCode == AppConstant.REQUEST_EDIT_TASK) {
			// If the task was edited successfully, go back to the list.
			if (resultCode == Activity.RESULT_OK) {
				taskDetailView.reLoadTask();
			}
		}
	}

	@Override
	public void editTask() {
		if (TextUtils.isEmpty(taskId)) {
			taskDetailView.showMissingTask();
			return;
		}
		taskDetailView.showEditTask(taskId);
	}

	@Override
	public void deleteTask() {
		if (TextUtils.isEmpty(taskId)) {
			taskDetailView.showMissingTask();
			return;
		}
		tasksDataSource.deleteTask(taskId);
		taskDetailView.showTaskDeleted();
	}

	@Override
	public void completeTask() {
		if (TextUtils.isEmpty(taskId)) {
			taskDetailView.showMissingTask();
			return;
		}
		tasksDataSource.completeTask(taskId);
		taskDetailView.showTaskMarkedComplete();
	}

	@Override
	public void activateTask() {
		if (TextUtils.isEmpty(taskId)) {
			taskDetailView.showMissingTask();
			return;
		}
		tasksDataSource.activateTask(taskId);
		taskDetailView.showTaskMarkedActive();
	}

	private void showTask(Task task) {
		String title = task.getTitle();
		String description = task.getDescription();

		if (TextUtils.isEmpty(title)) {
			taskDetailView.hideTitle();
		} else {
			taskDetailView.showTitle(title);
		}

		if (TextUtils.isEmpty(description)) {
			taskDetailView.hideDescription();
		} else {
			taskDetailView.showDescription(description);
		}
		taskDetailView.showCompletionStatus(task.isCompleted());
	}
}
